package com.mofeng.myshop.exception;

import com.mofeng.myshop.until.R;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
//@ControllerAdvice
@Order(Ordered.HIGHEST_PRECEDENCE)//优先级最高
public class GlobalExceptionHandler {

    @ExceptionHandler({MyException.class, RuntimeException.class})
    public R handleCustomException(RuntimeException e) {
        e.printStackTrace();
        return R.FAILED(e.getMessage());
    }

    @ExceptionHandler(AccessDeniedException.class)
    public R handleException(AccessDeniedException ex) {
        return R.PERMISSION_DENIED();
    }


    @ExceptionHandler(TimeoutException.class)
    public R handleException(TimeoutException ex) {
        ex.printStackTrace();
        R r = R.TIME_OUT();
        r.setMessage(ex.getMessage());
        return r;
    }


    @ExceptionHandler(NullPointerException.class)
    public R handleException(NullPointerException ex) {
        ex.printStackTrace();
        return R.FAILED("程序发生空指针异常");
    }

    @ExceptionHandler(Throwable.class)
    public R handleException(Throwable ex) {
        ex.printStackTrace();
        return R.FAILED("程序出现异常，请稍后重试或者联系管理员");
    }

    @ExceptionHandler(Exception.class)
    public R handleException(Exception ex) {
        ex.printStackTrace();
        return R.FAILED("程序出现异常，请稍后重试或者联系管理员");
    }
    // 可以继续定义其他的异常处理方法...
}